/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.web.controller;



import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.youth.fund.hk.hostManager.common.Constants;
import com.youth.fund.hk.hostManager.manager.datamodel.NewsModel;
import com.youth.fund.hk.hostManager.manager.service.NewsService;
import com.youth.fund.hk.hostManager.manager.service.UploadService;

/**
 * @author Sunk_Sky
 *
 *         2014年11月14日下午7:17:31
 */

@Controller
@RequestMapping("/news")
public class NewsController
{
	@Autowired
	@Qualifier("NewsService")
	private NewsService service;

	@Autowired
	@Qualifier("UploadService")
	private UploadService uploadService;

	@RequiresPermissions("news:view")
	@RequestMapping(method = RequestMethod.GET)
	public String list(HttpServletRequest req, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}
		NewsModel nm = new NewsModel();

		nm.setLang(lang);

		model.addAttribute("newsList", this.service.query(nm));

		return "news/list";
	}

	@RequiresPermissions("news:view")
	@RequestMapping(value = "/{language}", method = RequestMethod.GET)
	public String listLang(@PathVariable("language") String language,
			HttpServletRequest req, Model model)
	{
		String lang = language;

		req.getSession().setAttribute("local_lang", lang);

		NewsModel nm = new NewsModel();

		nm.setLang(lang);

		model.addAttribute("newsList", service.query(nm));

		return "news/list";
	}

	@RequiresPermissions("news:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String showCreateForm(HttpServletRequest req, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}
		setCommonData(model, lang);

		NewsModel nm = new NewsModel();

		nm.setLang(lang);

		model.addAttribute("news", nm);

		model.addAttribute("op", "新增");

		return "news/edit";
	}

	@RequiresPermissions("news:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(HttpServletRequest req, NewsModel model,
			RedirectAttributes redirectAttributes,
			@RequestParam("file") MultipartFile file)
	{
		String url = "";

		if (file.isEmpty())
		{
			redirectAttributes.addFlashAttribute("msg", "新增失敗,上传文件为空");

			return "redirect:/news";
		}

		url = this.uploadService.uploadPDF(file, model.getName());

		model.setContent(url);
		boolean result = this.service.create(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "新增成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "新增失敗");
		}

		return "redirect:/news";
	}

	@RequiresPermissions("news:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(HttpServletRequest req, @PathVariable int id,
			Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		setCommonData(model, lang);

		NewsModel nm = new NewsModel();

		nm.setLang(lang);

		nm.setId(id);

		model.addAttribute("news", service.queryOne(nm));

		model.addAttribute("op", "修改");

		return "news/edit";
	}

	@RequiresPermissions("news:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update( NewsModel model,
			RedirectAttributes redirectAttributes,
			@RequestParam("file") MultipartFile file)
	{
		String url = "";

		if (!file.isEmpty())
		{
			url = this.uploadService.uploadPDF(file, model.getName());
			
			model.setContent(url);
		}

		boolean result = service.update(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "修改成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "修改失敗");
		}

		return "redirect:/news";
	}

	@RequiresPermissions("news:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String showDeleteForm(HttpServletRequest req, @PathVariable int id,
			Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		setCommonData(model, lang);

		NewsModel nm = new NewsModel();

		nm.setId(id);

		nm.setLang(lang);

		model.addAttribute("news", service.queryOne(nm));

		model.addAttribute("op", "删除");

		return "news/edit";
	}

	@RequiresPermissions("news:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	public String delete(HttpServletRequest req, NewsModel model,
			RedirectAttributes redirectAttributes)
	{
		boolean result = service.delete(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "刪除成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "刪除失敗");
		}

		return "redirect:/news";
	}

	private void setCommonData(Model model, String lang)
	{
		NewsModel nm = new NewsModel();

		nm.setLang(lang);

		model.addAttribute("newsList", service.query(nm));
	}
}
